package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.bean.Student;
import com.itheima.mapper.StudentMapper;
import com.itheima.service.StudentService;
import com.itheima.utils.MapperUtils;

import java.util.List;

public class StudentServiceImpl implements StudentService {
    @Override
    public PageInfo<Student> findByPage(int currentPage, int pageSize) {

        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            //查询之前设置分页参数
            PageHelper.startPage(currentPage,pageSize);
            List<Student> studentList=studentMapper.findAll();
            //查询之后，基于studentList创建一个PageInfo对象返回
            return new PageInfo<>(studentList);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MapperUtils.close();
        }
        return  null;

    }
    /*添加学生信息*/
    @Override
    public Integer addStu(Student student) {
        try{
            StudentMapper studentMapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count =studentMapper.add(student);
            //提交
            MapperUtils.commit();
            return count;
        }catch(Exception e){
            e.printStackTrace();
            MapperUtils.rollback();
        }finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public Integer updateStu(Student student) {

        try {
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count=mapper.update(student);
            System.out.println("最初"+count);
            MapperUtils.commit();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtils.rollback();
        } finally {
            MapperUtils.close();
        }
        return null;
    }

    @Override
    public Integer deleteStu(String number) {
        try {
            StudentMapper mapper = MapperUtils.getMapper(StudentMapper.class);
            Integer count=mapper.delete(number);

            MapperUtils.commit();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
            MapperUtils.rollback();
        } finally {
            MapperUtils.close();
        }

        return null;
    }
}
